<?php  
import('miho.db.criteria.*');

/**
* 
*/
class Criteria
{
  
  protected $_joins;
  protected $_table;
  protected $_fields;
  protected $_criterions;
  
  /**
   * _addField
   *
   * add field to list of fields to get.
   *
   * @param BaseTableField $field 
   * @return void
   * @author miho
   */
  protected function _addField(BaseTableField $field){
    if(!in_array($field,$this->_fields)){
      $this->_fields[] = $field;
    }
  }
  
  protected function _addJoin(BaseTableField $first, BaseTableField $second){
    $this->_joins[] = new Join($first,$second);
  }
  
  public function __construct()
  {
    $this->_joins = array();
    $this->_fields = array();
  }
  
   /**
   * add
   * how this shoud work
   * $criteria = new Criteria();
   * $criteria->add($table1->name); // select name from table 1;
   * $criteria->add($table1->name,'test') //By default 3d parameter is Criteria::EQUAL,
   * $criteria->add($table1->name,'test',Criteria::NOT_EQUAL);
   * $criteria->add($table2->id,$table1->user_id) // joins table 2 on user_id=id
   *
   * @return void
   * @author miho
   */
   
  public function add(){
   $arguments = func_get_args();
   if(count($arguments)==1){
     $this->_addField($arguments[0]);
   }elseif(count($arguments)==2){
     if($argument[1] instanceof BaseTableField){
       $this->_addJoin($arguments[0],$arguments[1]);
     }
   }
   return $this;
  }
  
  public function generateSql(){
    $tables = array();
    $fields = '';
    foreach ($this->_fields as $field) {
      if(!in_array($field->getTable(),$tables)){
        $tables[]=$field->getTable();
      }
      $fields .= ($fields=='' ? ' ' : ', ').$field->getTable()->getName().'.'.$field->getName().' as '.$field->getTable()->getName().'_'.$field->getName();
    }
    $tables_s = '';
    foreach ($tables as $table) {
      $tables_s .= ($tables_s=='' ? ' ' : ', ').$table->getName();
    }
    $sql = 'SELECT '.$fields.' FROM '.$tables_s.' ;';
    var_dump($sql);
    return $sql;
  }
  
  public function where(){
    return $this;
  }
  
  public function orderBy(){
    return $this;
  }
  
  public function limit(){
    return $this;
  }
  
  public function groupBy(){
    return $this;
  }
  
  public function join(){
    return $this;
  }
  
  
}


?>